/**   
 * Copyright © 2016 北京之洲科技有限责任公司. All rights reserved.
 * 
 * @Title: ChoiceIDCrawlOfAutoHome.java 
 * @Prject: carfactory
 * @Package: com.zizo.autohome.choice.crawl 
 * @Description: 
 * @author: Alexis  
 * @email heyan0227@outlook.com
 * @date: 2016年8月8日 下午12:11:29 
 * @version: V1.0   
 */
package com.zizo.autohome.choice.crawl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.zizo.util.ConnectionPool;
import com.zizo.util.DocUtil;

/**
 * @ClassName: ChoiceIDCrawlOfAutoHome
 * @Description:
 * @author: Alexis
 * @date: 2016年8月8日 下午12:11:29
 */
public class ChoiceIDCrawlOfAutoHome {
	private Connection conn = ConnectionPool.getConnection("autohome", "carvoice.tpddns.cn", "3308");

	/**
	 * @Title: getChoiceID
	 * @Description: 得到一级分类的ID
	 * @return
	 * @return: List<String>
	 * @author: Alexis
	 */
	public List<String> getChoiceID() {
		List<String> ids = new ArrayList<>();
		Document doc = DocUtil.getDoc("http://club.autohome.com.cn/jingxuan");
		Elements lis = doc.select("ul.section-list li dd:not(.fixed-right)");
		for (Element li : lis) {
			String id = li.attr("id").split("_")[1];
			String name = li.select("a").text();
			insertID(id, name);
		}
		return ids;
	}

	/**
	 * @Title: insertID
	 * @Description: 插入数据库qczj_choice_id表中
	 * @param id
	 * @param name
	 * @return: void
	 * @author: Alexis
	 */
	public void insertID(String id, String name) {
		PreparedStatement ps = null;
		String sql = "REPLACE INTO qczj_choice_id(id,name) VALUES(?,?)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, Integer.parseInt(id));
			ps.setString(2, name);
			ps.executeUpdate();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		ChoiceIDCrawlOfAutoHome choice = new ChoiceIDCrawlOfAutoHome();
		choice.getChoiceID();
	}
}
